<template>
  <ui-page class="container">
    <div v-for="chart in chartTypes" :key="chart.ref" class="chart-item">
      <div class="chart-item-title">{{ chart.name }}</div>
      <div class="chart-item-chart">
        <v-chart :chart="chart.ref" :data="getChartData(chart)"/>
      </div>
    </div>
  </ui-page>
</template>

<script>
import ChartData from '@/assets/data/chart.json';

export default {
  name: "picChart",
  data() {
    return {
      chartTypes: [
        {name: 'P0001', ref: 'P0001', data: {}},
        {name: 'P0002', ref: 'P0002', data: {}},
        {name: 'P0003', ref: 'P0003', data: {}},
        {name: '环形图2', ref: 'pie-nested-legend', data: {}},
        {name: 'P0004', ref: 'P0004', data: {}},
        {name: 'P0005', ref: 'P0005', data: {}},
        {name: 'P0006', ref: 'P0006', data: {}},
        {name: 'P0007', ref: 'P0007', data: {}},
        {name: 'P0008', ref: 'P0008', data: {}},
      ],
      tooltip: null
    }
  },
 
  methods: {
    getChartData(chart) {
      if(chart.ref === 'P0003') {
        return this.getPieNested(chart);
      } else if (chart.ref === 'pie-nested-legend') {
        return this.getPieNestedTitleLegend(chart);
      } else if (chart.ref === 'P0002') {
        return this.getPieBaseTitleLegend(chart);
      } else if (chart.ref === 'P0007') {
        return {data:ChartData[chart.ref]}
      }else if (chart.ref === 'P0004') {
        return {data:ChartData[chart.ref]}
      }else if (chart.ref === 'P0005') {
        return {data:ChartData[chart.ref]}
      }else if (chart.ref === 'P0006') {
        return {data:ChartData[chart.ref]}
      }else if (chart.ref === 'P0008') {
        return ChartData[chart.ref];
      }
      return ChartData[chart.ref];

    },
    getPieNested(chart) {
      const title =  [
        {
          text: 100,
          x: '48%',
          top: '40%',
          textAlign:'center',
          textStyle: {
            color: '#fff',
            fontSize: 16,
            fontWeight: '500',
          },
        },
        {
          text: '报警总数',
          x: '49%',
          top: '50%',
          textAlign:'center',
          textStyle: {
            fontSize: 12,
            color: 'rgba(255, 255, 255, 0.6)',
            fontWeight: '500',
          },
        },
      ];

      return {data: ChartData[chart.ref], title};
    },
    getPieNestedTitleLegend(chart) {
      const title =   [
        {
          text: 100,
          x: '23%',
          top: '40%',
          textAlign:'center',
          textStyle: {
            color: '#fff',
            fontSize: 16,
            fontWeight: '500',
          },
        },
        {
          text: '报警总数',
          x: '24%',
          top: '50%',
          textAlign:'center',
          textStyle: {
            fontSize: 12,
            color: 'rgba(255, 255, 255, 0.6)',
            fontWeight: '500',
          },
        },
      ];
      const legend = {
        top: '18%'
      };
      return {data: ChartData[chart.ref], title, legend};
    },
    getPieBaseTitleLegend(chart) {
      return {data: ChartData[chart.ref]};
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;

  .chart-item {
    width: 360px;
    height: 250px;
    margin: 20px;
    background: #244258;
    border-radius: 10px;
    display: flex;
    flex-direction: column;

    .chart-item-title {
      text-align: center;
      line-height: 30px;
      color: #ffffff;
    }

    .chart-item-chart {
      flex: 1;
    }
  }
}
</style>
